local ExcelTaskConf = require("excel.Task")
local DB = require("common.DB")
local LuaMongo = _G.lua_mongo
Json = Json or require("common.Json")
local Msg = require("core.Msg")


function synsTaskPoolToLogic(list)
   local msgRet = Msg.wl.WL_TASK_GROUP_POOL
   msgRet.data = Json.Encode(list)
   for i = 0, 3 do
      Msg.send2logic_id(msgRet, i)
   end
end

function initTaskPool()
  local task_pool_data = {}
  for groupID, groupConfig in pairs(ExcelTaskConf.TaskGroup) do
     if task_pool_data[groupID] == nil then
      LuaMongo.find(DB.db_taskPool, {groupID=groupID})
      local poolData = {}
      if not LuaMongo.next(poolData) then
        local tasks = groupConfig.groupContent[math.random(1, #groupConfig.groupContent)]
        task_pool_data[groupID] = tasks
        LuaMongo.insert(DB.db_taskPool, {groupID = groupID, tasks = tasks})
      else
        task_pool_data[groupID] = poolData.tasks
      end
     end
  end
  synsTaskPoolToLogic(task_pool_data)
end

function refreshTaskGroupPool()
  local task_pool_data = {}
  for groupID, groupConfig in pairs(ExcelTaskConf.TaskGroup) do
      local tasks = groupConfig.groupContent[math.random(1, #groupConfig.groupContent)]
      task_pool_data[groupID] = tasks
      LuaMongo.update(DB.db_taskPool, {groupID=groupID}, {groupID = groupID, tasks = tasks})
  end
  synsTaskPoolToLogic(task_pool_data)
end
